﻿using System.Web.Mvc;
using InvoiceInvoker.Logic;
using InvoiceInvoker.Logic.Repositories;
using InvoiceInvoker.MvcUI.Filters;
using InvoiceInvoker.MvcUI.Helpers;
using InvoiceInvoker.MvcUI.Models;
using InvoiceInvoker.MvcUI.ViewModels;

namespace InvoiceInvoker.MvcUI.Controllers
{
	[Authorize]
	[HandleError]
	public class ProductsController : Controller
	{
		private int _sellerId;
		private RegisteredProductRepository _repository;

		protected override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			this.SetSellerId();
			_sellerId = (int)Session["SellerId"];
			_repository = new RegisteredProductRepository(_sellerId);

			base.OnActionExecuting(filterContext);
		}

		//
		// GET: /Products/

		public ActionResult Index(ProductsIndexViewModel viewModel)
		{
			if (viewModel.Filter == null)
				viewModel.Filter = new ProductsFilter();

			viewModel.Products = _repository.GetByExpression(viewModel.Filter.GetExpression());

			return View(viewModel);
		}

		//
		// GET: /Products/Create

		public ActionResult Create()
		{
			ProductsViewModel viewModel = new ProductsViewModel
			{
				Product = new ProductModel(_sellerId),
				VatRates = VatRatesProvider.GetVatRates()
			};

			return View(viewModel);
		}

		//
		// POST: /Products/Create

		[HttpPost]
		public ActionResult Create(ProductsViewModel viewModel)
		{
			if (ModelState.IsValid)
			{
				if (string.IsNullOrWhiteSpace(viewModel.Product.Pkwiu))
					viewModel.Product.Pkwiu = "-";

				viewModel.Product.NetPrice = decimal.Round(viewModel.Product.NetPrice, 2);

				_repository.Add(viewModel.Product.ToRegisteredProduct());

				return RedirectToAction("Index");
			}
			else
			{
				viewModel.VatRates = VatRatesProvider.GetVatRates();

				return View(viewModel);
			}
		}

		//
		// GET: /Products/Edit/5

		public ActionResult Edit(int id)
		{
			ProductsViewModel viewModel = new ProductsViewModel
			{
				Product = new ProductModel(_repository.GetById(id)),
				VatRates = VatRatesProvider.GetVatRates()
			};

			return View(viewModel);
		}

		//
		// POST: /Products/Edit/5

		[HttpPost]
		public ActionResult Edit(int id, ProductsViewModel viewModel)
		{
			if (ModelState.IsValid)
			{
				if (string.IsNullOrWhiteSpace(viewModel.Product.Pkwiu))
					viewModel.Product.Pkwiu = "-";

				viewModel.Product.NetPrice = decimal.Round(viewModel.Product.NetPrice, 2);

				_repository.Update(viewModel.Product.ToRegisteredProduct());

				return RedirectToAction("Index");
			}
			else
			{
				viewModel.VatRates = VatRatesProvider.GetVatRates();

				return View(viewModel);
			}
		}

		//
		// GET: /Products/Delete/5

		public ActionResult Delete(int id)
		{
			ProductModel product = new ProductModel(_repository.GetById(id));

			return View(product);
		}

		//
		// POST: /Products/Delete/5

		[HttpPost]
		public ActionResult Delete(int id, FormCollection collection)
		{
			_repository.Remove(id);

			return RedirectToAction("Index");
		}
	}
}
